#pragma once

#include <QtWidgets/QMainWindow>
#include <qserialport.h>
#include <qserialportinfo.h>
#include <qtimer.h>
#include <qsystemtrayicon.h>
#include <qicon.h>
#include <qevent.h>
#include <vector>
#include <thread>
#include <map>
#include "ui_wireless_system.h"
#include "time_capture.h"
#include "node_data.h"

enum RankRule
{
	NODE_ORDER,
	TIME_UP,
	TIME_DOWN
};

class wireless_system : public QMainWindow
{
	Q_OBJECT

public:
	wireless_system(QWidget *parent = Q_NULLPTR);

	QColor electric_color;
	QColor no_electric_color;
	QColor future_color;
	QColor leaf_text_color;
	QColor user_lable_color;
	QColor total_lable_color;
	QColor boundary_line_color;

protected:
	bool eventFilter(QObject *currentWidget, QEvent *event);


private slots:
	void TabWidgetTabClickSlots(int current_index);

	void ActionSettingLableTriSlots();
	void ActionDayTriSlots();
	void ActionRankTriSlots();
	void ActionSettingProfile();
	void ActionHelpDocument();

	void ActionTrayShow();

	void PushSureSaveLable();
	void PushCancleSaveLable();
	void PushCancleSetFormat();
	void PushSaveSetFormat();
	void PushToday();
	void PushRuleId();
	void PushRuleUp();
	void PushRuleDown();
	void PushBuildRoute();
	void PushSerialConnect();
	void PushSetBeginDate();

	void BoxYearChange(int index);
	void BoxMonthChange(int index);
	void BoxDayChange(int index);

	void BoxRankYearFChange(int index);
	void BoxRankMonthFChange(int index);
	void BoxRankDayFChange(int index);

	void BoxRankYearTChange(int index);
	void BoxRankMonthTChange(int index);
	void BoxRankDayTChange(int index);

	void readSerialSlot();
	void PushSerialSend();
	void PushCleanReceive();

private:
	void closeEvent(QCloseEvent *event);

	void initWidget();

	void initTray();

	void openSerialPort();
	void closeSerialPort();

	void BoxAddYearTPL(dateData begin_date, dateData end_date, QComboBox* box_ptr);
	void BoxAddMonthTPL(dateData begin_date, dateData end_date, int current_year, QComboBox* box_ptr);
	void BoxAddDayTPL(dateData begin_date, dateData end_date, int current_year, int current_month, QComboBox* box_ptr);

	void updateRankDateBox();
	void updateRandDateBoxF();
	void updateRandDateBoxT();
	void updateRankTable();

	void updateDateBoxes();
	void updateSetForamtWidget();
	void updateTabWidget();
	void updateUserLableTable();
	
	void drawBoundary(int page_order);
	void drawDayAnnu(int row, int col, int page_order, DayData timeList);
	void drawLeafTotalText(QPainter& painter, DayData timeList, int center_x, int center_y, int over_radius, int total_minute);
	void drawLeafAnnu(QPainter& painter, int center_x, int center_y, float start_angle, float end_angle, int diff_radius, int over_radius, QColor fill_color);
	void drawWholeAnnu(QPainter& painter, DayData timeList, int x, int y, int diff_radius, int over_radius, bool is_begin_date = false, bool is_end_date = false);
	void drawWholeAnnu(QPainter& painter, QColor draw_color, int x, int y, int diff_radius, int over_radius);
	void drawUserLable(QPainter& painter, int x, int y, int redius, std::string lable);
	void drawBarRolling();

	int annu_rows;
	int annu_cols;
	uint8_t rank_method;
	bool serial_isopened;
	std::string current_port_str;
	
	dateData rank_date_f;
	dateData rank_date_t;
	dateData print_date;

	QIcon icon;
	QSerialPort* serial_port_ptr;
	QSystemTrayIcon* tray_app;
	Ui::wireless_systemClass ui;
};
